home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 66587 / 66587.xpi / content / overlay.js next >
Text File  |  2010-02-05  |  7KB  |  133 lines

  1. var panabeemini = {
  2.     old_url: null,
  3.         all_panels: new Array(), // Matches Panabee pages to the right deck panel
  4.     
  5.     onLoad: function(){
  6.         // Initialization code
  7.         this.initialized = true;
  8.         this.strings = document.getElementById("panabeemini-strings");
  9.         var appcontent = document.getElementById("appcontent"); // Browser
  10.         if (appcontent) {
  11.             appcontent.addEventListener('DOMContentLoaded', this.onDomContentLoaded, false);           
  12.         }
  13.         var main_window = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor).getInterface(Components.interfaces.nsIWebNavigation).QueryInterface(Components.interfaces.nsIDocShellTreeItem).rootTreeItem.QueryInterface(Components.interfaces.nsIInterfaceRequestor).getInterface(Components.interfaces.nsIDOMWindow);        
  14.         main_window.gBrowser.addProgressListener(panabeemini_urlBarListener, Components.interfaces.nsIWebProgress.NOTIFY_LOCATION); 
  15.         
  16.         },
  17.         
  18.     uninit: function(){
  19.         if (gBrowser) gBrowser.removeProgressListener(panabeemini_urlBarListener);
  20.     },
  21.         
  22.     onDomContentLoaded: function(event){
  23.             var doc = event.originalTarget;    
  24.       if (doc.nodeName == '#document' && doc == content.document && panabeemini.is_results_page(content.document.location)) { // Show sidebar
  25.                     panabeemini.show_results();
  26.       } else if (doc.nodeName == '#document' && doc == content.document && content.document.location.href.indexOf('http://www.panabee.com/s?') != -1) { // On Panabee results page
  27.                 var iframes = content.document.getElementsByTagName('iframe');
  28.                 for (var i = 0; i < iframes.length; i++) {
  29.                     iframes[i].contentDocument.wrappedJSObject.body.addEventListener('click', function(event) { var link = null; if (event.originalTarget.href) { link = event.originalTarget; } else if (event.originalTarget.parentNode && event.originalTarget.parentNode.href) { link = event.originalTarget.parentNode; } if (link) { var d = new Date(); link.setAttribute('target', '_newtab' + d.getTime()); }}, false);    
  30.                 }
  31.             }
  32.     },
  33.                
  34.         open_link: function (event) {            
  35.             gBrowser.selectedTab = gBrowser.addTab(event.originalTarget.panabeemini_link);
  36.             //gBrowser.selectedTab = gBrowser.addTab('http://www.google.com');
  37.         },
  38.         
  39.     show_results: function(panabeemini_doc){
  40.             if (!panabeemini_doc) {
  41.                 var panel = this.all_panels[gBrowser.selectedTab.panabeemini]; // Find the right panel
  42.                 panabeemini_doc = panel.contentDocument;
  43.                 var deck = document.getElementById('panabeemini_content_deck');
  44.                 deck.selectedPanel = panel; // CH: redundant?                
  45.             }
  46.             var links_div = panabeemini_doc.getElementById("main_page_links");
  47.             var loc_div = panabeemini_doc.getElementById("main_page_loc");                
  48.             if (links_div && loc_div) {    // Is page loaded
  49.                 var main_page_links = content.document.getElementsByTagName('a'); // Links in main page
  50.                 links_div.innerHTML += '<panabeemini>'; // Delimit links
  51.                 for (var i = 0; i < main_page_links.length; i++) {
  52.                     links_div.innerHTML += " " + main_page_links[i];
  53.                 }
  54.                 loc_div.innerHTML = content.document.location.toString();
  55.                 var ev = document.createEvent('HTMLEvents');
  56.                 ev.initEvent('panabeemini_show', true, true);
  57.                 panabeemini_doc.dispatchEvent(ev);
  58.             }        
  59.     },
  60.  
  61.         // Throws nasty exception when loc is about:blank    
  62.         is_results_page: function(loc) {                        
  63.                 return (loc && loc.toString() != 'about:blank' && loc.hostname.indexOf('.google.com') != -1 && (loc.search.indexOf('q=') != -1 || loc.hash.indexOf('q=') != -1));            
  64.         },
  65.         
  66.         show_results_wrapper: function(event) {
  67.             event.originalTarget.addEventListener('panabeemini_open_link', panabeemini.open_link, false);
  68.             panabeemini.show_results(event.originalTarget);
  69.         },
  70.                 
  71.     process_new_url: function(aURI){      
  72.             if (aURI.spec != this.old_url) { // New URL
  73.                 this.old_url = aURI.spec;
  74.                 var main_loc = content.document.location;
  75.           if (panabeemini.is_results_page(main_loc)) { // Google search result page
  76.                     //alert('process');
  77.                     var root_url = 'http://www.panabee.com/mini?q=';
  78.                     //var root_url = 'http://localhost:3000/mini?q=';
  79.                     var panabee_url = root_url + encodeURIComponent(content.document.location);
  80.                     var deck = document.getElementById('panabeemini_content_deck');
  81.                     if (!gBrowser.selectedTab.panabeemini) {
  82.                         var d = new Date();
  83.                         gBrowser.selectedTab.panabeemini = d.getTime(); // Assign unique ID to selected tab.  Yikes.
  84.                     }
  85.                     var one_panel = this.all_panels[gBrowser.selectedTab.panabeemini]; // Find the right panel, if it exists, within the deck  
  86.                     if (!one_panel) { // Add panel to deck
  87.                         var new_iframe = document.createElement('iframe');
  88.                         deck.appendChild(new_iframe);
  89.                         new_iframe.contentWindow.addEventListener('load', panabeemini.show_results_wrapper, false);
  90.                         new_iframe.setAttribute('src', panabee_url);
  91.                         this.all_panels[gBrowser.selectedTab.panabeemini] = new_iframe;
  92.                     }
  93.                     deck.selectedPanel = one_panel;
  94.                     document.getElementById('panabeemini_sidebar').collapsed = false;
  95.                     if (content.document.location.hash) { // DOMContentLoaded fires for non-AJAX pages.  If hash is present, invoke show_results directly since DOMContentLoaded won't fire.
  96.                         setTimeout(panabeemini.show_results_after_delay, 1000); // Delay in order to let AJAX calls complete
  97.                     }
  98.                 } else {
  99.                     //alert('process');
  100.                     document.getElementById('panabeemini_sidebar').collapsed = true;
  101.                 }
  102.       }
  103.     },
  104.  
  105.         show_results_after_delay: function() {
  106.             panabeemini.show_results();
  107.         },
  108.  
  109. };
  110.  
  111. var panabeemini_urlBarListener = {
  112.     QueryInterface: function(aIID){
  113.       if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
  114.       aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
  115.       aIID.equals(Components.interfaces.nsISupports)) 
  116.           return this;
  117.       throw Components.results.NS_NOINTERFACE;
  118.     },
  119.     
  120.     onLocationChange: function(progress, aRequest, aURI){
  121.             if (progress.DOMWindow.parent == progress.DOMWindow) { // Only watch windows that are their own parent -- e.g. not frames
  122.                 panabeemini.process_new_url(aURI);    
  123.             }
  124.             
  125.     },
  126.     
  127.     onStateChange: function(a, b, c, d){},
  128.     onProgressChange: function(a, b, c, d, e, f){},
  129.     onStatusChange: function(a, b, c, d){},
  130.     onSecurityChange: function(a, b, c){},
  131. };
  132.  
  133. window.addEventListener('load', function(e){ panabeemini.onLoad(e); }, false);